package foo.bar.blog.web.nonextension;

import net.sourceforge.stripes.action.Resolution;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedOutput;

import java.io.BufferedWriter;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class RomeSyndicationResolution implements Resolution {

    private SyndFeed syndFeed;

    public RomeSyndicationResolution(SyndFeed syndFeed) {
        this.syndFeed = syndFeed;        
    }

    public void execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        SyndFeedOutput syndFeedOutput = new SyndFeedOutput();
        String output = syndFeedOutput.outputString(syndFeed);
        if (syndFeed.getFeedType().startsWith("atom")) {
            response.setContentType("application/atom+xml");
        }
        if (syndFeed.getFeedType().startsWith("rss")) {
            response.setContentType("application/rss+xml");
        }
        response.setContentLength(output.getBytes().length);
        response.getOutputStream().write(output.getBytes("UTF-8"));
        response.flushBuffer();
    }
}
